const Base = require('./base.js');
module.exports = class extends Base {
  __before() {
    super.__before();
    this.adminService = think.rpc.admin;
  }
  // 修改密码
  async changePwdAction() {
    const userid = this.getUserId();
    const pwd = this.trimVal('pwd');
    const oldpwd = this.trimVal('oldpwd');
    const admin = await this.adminService.queryById(userid);
    if (think.isEmpty(admin)) {
      return this.fail('账号不存在');
    }
    if (admin.status === 0) {
      return this.fail('账号已被冻结');
    }
    if (think.md5(oldpwd) === admin.pwd) {
      return this.fail('旧密码不正确');
    }
    if (pwd === oldpwd) {
      return this.fail('新密码和旧密码不能一样');
    }
    await this.adminService.updateById(this.getPlatformId(), userid, {
      pwd: think.md5(pwd),
      modify_time: think.datetime()
    });
    return this.success();
  }
  // 充值密码
  async resetPwdAction() {
    const userid = this.intVal('id');
    const pwd = think.md5('123456');
    await this.adminService.updateById(this.getPlatformId(), userid, {
      pwd,
      modify_time: think.datetime()
    });
    return this.success();
  }
  // 查询
  async queryDataAction() {
    const username = this.trimVal('username');
    const page = this.intVal('page');
    const limit = this.intVal('limit');
    const data = await this.adminService.queryData(this.getPlatformId(), {
      username,
      page,
      limit
    });
    return this.success(data);
  }
  // 删除
  async removeAction() {
    const id = this.intVal('id');
    await this.adminService.removeById(this.getPlatformId(), id);
    return this.success();
  }
  // 冻结
  async frozenAction() {
    const id = this.intVal('id');
    await this.adminService.updateById(this.getPlatformId(), id, {
      status: 0,
      modify_time: this.datetime()
    });
    return this.success();
  }
  // 解冻
  async unfrozenAction() {
    const id = this.intVal('id');
    await this.adminService.updateById(this.getPlatformId(), id, {
      status: 1,
      modify_time: this.datetime()
    });
    return this.success();
  }
};
